package com.myopen.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.myopen.converter.ListConverter;
import com.myopen.kit.UUIDKit;

public class WebsiteContact extends Model<WebsiteContact>{

	private static final long serialVersionUID = 1490677305680961009L;
	public static final WebsiteContact dao = new WebsiteContact();
	
	public Page<Record> pagequery(Record record){
		StringBuffer sqlExceptSelect=new StringBuffer("from t_website_contact t "
				+ " where t.removed='0' ");
		List<Object> list=new ArrayList<Object>();
		
		if(StrKit.notBlank(record.getStr("searchContent"))){
			sqlExceptSelect.append(" and (t.name like ? or t.email like ? or t.phone like ? or t.address like ? or t.message like ?) ");
			list.add("%"+record.getStr("searchContent")+"%");
			list.add("%"+record.getStr("searchContent")+"%");
			list.add("%"+record.getStr("searchContent")+"%");
			list.add("%"+record.getStr("searchContent")+"%");
			list.add("%"+record.getStr("searchContent")+"%");
		}
		
		sqlExceptSelect.append(" order by t.createTime desc");
		Page<Record> page= Db.paginate(record.getInt("pageNumber"),record.getInt("pageSize"),"select t.*",sqlExceptSelect.toString(),list.toArray());		
		String[] dicTypeKeys= {};
		String[] dicTypes= {};
		
		return new ListConverter<Record>(page,dicTypeKeys,dicTypes).getPage();
	}
	
	public WebsiteContact save(WebsiteContact vo,Record currUser){
		
		WebsiteContact bo=null;
		if(StrKit.isBlank(vo.getStr("id"))){
			bo=new WebsiteContact();
			
			bo.set("id", UUIDKit.getUUID());
			bo.set("removed", "0");
			bo.set("createTime", new Date());
			bo.set("creator", currUser.getStr("id"));
			bo.set("updateTime", new Date());
			bo.set("updator", currUser.getStr("id"));
			
			bo.set("name", vo.getStr("name"));
			bo.set("email", vo.getStr("email"));
			bo.set("phone", vo.getStr("phone"));
			bo.set("address", vo.getStr("address"));
			bo.set("message", vo.getStr("message"));
			
			bo.save();
		}else{
			bo=WebsiteContact.dao.findById(vo.getStr("id"));
			bo.set("updateTime", new Date());
			bo.set("updator", currUser.getStr("id"));
			
			bo.set("name", vo.getStr("name"));
			bo.set("email", vo.getStr("email"));
			bo.set("phone", vo.getStr("phone"));
			bo.set("address", vo.getStr("address"));
			bo.set("message", vo.getStr("message"));

			bo.update();
		}
		
		return bo;
		
	}
}
